package com.ruoyi.business.service.product.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.product.TbProductSpecTemplates;
import com.ruoyi.business.mapper.product.TbProductSpecTemplatesMapper;
import com.ruoyi.business.service.product.TbProductSpecTemplatesService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 规格分类模板表Service实现类
 */
@Service
@RequiredArgsConstructor
public class TbProductSpecTemplatesServiceImpl extends ServiceImpl<TbProductSpecTemplatesMapper, TbProductSpecTemplates>
        implements TbProductSpecTemplatesService {
    private final TbProductSpecTemplatesMapper baseMapper;
    @Override
    public List<TbProductSpecTemplates> selectSpecTemplatesList(TbProductSpecTemplates specTemplates) {
        return this.list();
    }

    @Override
    public TbProductSpecTemplates selectSpecTemplatesById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean insertSpecTemplates(TbProductSpecTemplates specTemplates) {
        return this.save(specTemplates);
    }

    @Override
    public boolean updateSpecTemplates(TbProductSpecTemplates specTemplates) {
        return this.updateById(specTemplates);
    }

    @Override
    public boolean deleteSpecTemplatesById(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean deleteSpecTemplatesByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public TableDataInfo<TbProductSpecTemplates> selectPageList(TbProductSpecTemplates tbProductSpecTemplates, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectPage(pageQuery.build(), this.getQueryWrapper(tbProductSpecTemplates)));
    }

    private LambdaQueryWrapper<TbProductSpecTemplates> getQueryWrapper(TbProductSpecTemplates tbProductSpecTemplates) {
        return Wrappers.lambdaQuery(TbProductSpecTemplates.class)
            .eq(tbProductSpecTemplates.getCategoryId()!=null,TbProductSpecTemplates::getCategoryId, tbProductSpecTemplates.getCategoryId())
            .like(StringUtils.isNotBlank(tbProductSpecTemplates.getName()),TbProductSpecTemplates::getName, tbProductSpecTemplates.getName());
    }
}
